from string import Template
import time
import urllib
import urllib2
import logging


from sqlalchemy import *
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from   urldb.model.url import  *
from parse_service_base import  *


class CallServiceBase(object):

    def __init__(self):
        ''' expect valid definitions for the following via subclass constructor
        '''
        url_path = '/ ?'
        control = None
        domain = ''
        parser = None
        urls = None

    def persist(self):
        ''' Put our array of Trips into the database... 
        '''
        if len(self.parser.trips) > 0:
            ses = self.control.get_session()
            ses.add_all(self.parser.trips)
            ses.commit()

    def build_url(self, urldb_url, mapping=None, filter=set(['date', 'Date'])):
        ''' takes a urldb.Url object, a dict (map params from X to Y, ala {'X':'Y'}, and a set of strings
        '''
        param_dict = Url.params_to_dict(urldb_url.params, mapping, filter)
        path = self.domain + self.url_path
        params = urllib.urlencode(param_dict)
        url = path + params
        return {'url':url, 'params':params, 'record':urldb_url}


    def find_urls(self, mapping=None, filter=set(['date', 'Date']), max=None):
        ''' 
            NOTE: we filter the 'date' parameter by default and return it as a 
                  special param ... to specify other filters, do filter=set(['time']) 
        '''
        ses = self.control.get_session()
        urls = ses.query(Url)
        retVal = []
        for u in urls:
            url_dict = self.build_url(u, mapping, filter)
            retVal.append(url_dict)

        return retVal

    def get_urls(self, mapping=None, filter=set(['date', 'Date']), max=None):
        '''
        '''
        if self.urls is None:
            self.urls = self.find_urls(mapping, filter)
        return self.urls


    def call_url(self, url, url_rec=None, params=None):
        ''' calls the trip web service, calls the XML (JSON) parser, returns a trip DAO
        '''
        trip = None
        try:
            start = time.time()
            req = urllib2.Request(url, None, {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'})
            socket = urllib2.urlopen(req)
            doc = socket.read()
            socket.close()
            end = time.time()
            trip = self.parser.parse(doc, url, url_rec)
            trip.response_time = end - start
            trip.params = params
            logging.info("call_url: response time of " + str(trip.response_time) + " seconds for url " + url)
        except:
            print 'ERROR: could not get data from url:\n', url, '\n(not a friendly place)'
            pass

        return trip


    def echoFirstUrl(self):
        ''' kind of a test to echo the first Url entry from the db
        '''
        ses = self.control.get_session();
        u = ses.query(Url)[0]
        print 'URL:', u.url, u.created
        for r in u.params:
            print ' ID:', r.id, ' name:', r.name, ' val:', r.value
